//
//  WDTaskDetailBasicInfoView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/29.
//

#import "WDTaskDetailBasicInfoView.h"
#import "WDArrivalServiceDetailModel.h"

@interface WDTaskDetailBasicInfoView ()

@property (nonatomic, strong) WDArrivalServiceDetailModel *taskDetailModel;

/// 车牌号
@property (nonatomic, strong) WDTaskDetailBasicSingleView *plateNumberView;

/// 服务类型
@property (nonatomic, strong) WDTaskDetailBasicSingleView *serviceTypeView;

/// 预约方式
@property (nonatomic, strong) WDTaskDetailBasicSingleView *appointWayView;

/// 工单类型
@property (nonatomic, strong) WDTaskDetailBasicSingleView *taskTypeView;

/// 当前状态
@property (nonatomic, strong) WDTaskDetailBasicSingleView *servFlowView;

/// 用户姓名
@property (nonatomic, strong) WDTaskDetailBasicSingleView *userNameView;

/// 用户电话
@property (nonatomic, strong) WDTaskDetailBasicSingleView *userPhoneView;

/// 购车时间
@property (nonatomic, strong) WDTaskDetailBasicSingleView *buyCarDateView;

/// 行车里程
@property (nonatomic, strong) WDTaskDetailBasicSingleView *mileageView;

/// 维修项目
@property (nonatomic, strong) WDTaskDetailBasicSingleView *repairItemsView;

/// 用户反馈
@property (nonatomic, strong) WDTaskDetailBasicSingleView *feedbackView;

/// 照片
@property (nonatomic, strong) WDTaskDetailBasicSingleImageView *picImageView;


/// 购车时间
- (void)initWithBuyCarDateView;

/// 行车里程
- (void)initWithMileageView;

/// 维修项目
- (void)initWithRepairItemsView;

/// 用户反馈
- (void)initWithFeedbackView;

@end

@implementation WDTaskDetailBasicInfoView

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor orangeColor];
}

/// 车牌号
- (void)initWithPlateNumberView {
    if (!_plateNumberView) {
        CGRect frame = CGRectMake(0, 0, self.width, kWDTaskDetailBasicSingleViewHeight);
        _plateNumberView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_plateNumberView];
    }
    if (self.taskDetailModel) {
        [_plateNumberView updateWithViewTitle:@"车牌号码" value:self.taskDetailModel.plateNumber valueHeight:18];
    }
}

/// 服务类型
- (void)initWithServiceTypeView {
    if (!_serviceTypeView) {
        CGRect frame = self.plateNumberView.frame;
        frame.origin.y = self.plateNumberView.bottom;
        _serviceTypeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_serviceTypeView];
    }
    if (self.taskDetailModel) {
        [_serviceTypeView updateWithViewTitle:@"服务类型" value:self.taskDetailModel.servTypeValue valueHeight:18];
    }
}

/// 预约方式
- (void)initWithAppointWayView {
    if (!_appointWayView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.serviceTypeView.bottom;
        _appointWayView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_appointWayView];
    }
    if (self.taskDetailModel) {
        [_appointWayView updateWithViewTitle:@"预约方式" value:self.taskDetailModel.orderTypeValue valueHeight:18];
    }
}

/// 工单类型
- (void)initWithTaskTypeView {
    if (!_taskTypeView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.appointWayView.bottom;
        _taskTypeView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_taskTypeView];
    }
    if (self.taskDetailModel) {
        [_taskTypeView updateWithViewTitle:@"工单类型" value:self.taskDetailModel.taskTypeValue valueHeight:18];
    }
}

/// 当前状态
- (void)initWithServFlowView {
    if (!_servFlowView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.taskTypeView.bottom;
        _servFlowView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_servFlowView];
    }
    if (self.taskDetailModel) {
        NSString *str = objectNull(self.taskDetailModel.servFlowValue);
        if ([self.taskDetailModel.auditStatus integerValue] == 1) {
            if (self.taskDetailModel.applyType && [self.taskDetailModel.applyType integerValue] == 0) {
                str = @"已申请救援";
            } else if (self.taskDetailModel.applyType && [self.taskDetailModel.applyType integerValue] == 1) {
                str = @"已申请取消救援";
            } else if (self.taskDetailModel.applyType && [self.taskDetailModel.applyType integerValue] == 2) {
                str = @"已申请关闭工单";
            }
        }
        [_servFlowView updateWithViewTitle:@"当前状态" value:str valueHeight:18];
    }
}

/// 用户姓名
- (void)initWithUserNameView {
    if (!_userNameView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.servFlowView.bottom;
        _userNameView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_userNameView];
    }
    if (self.taskDetailModel) {
        [_userNameView updateWithViewTitle:@"用户姓名" value:self.taskDetailModel.repairDriver valueHeight:18];
    }
}

/// 用户电话
- (void)initWithUserPhoneView {
    if (!_userPhoneView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.userNameView.bottom;
        _userPhoneView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_userPhoneView];
    }
    if (self.taskDetailModel) {
        [_userPhoneView updateWithViewTitle:@"用户电话" value:self.taskDetailModel.repairPhone valueHeight:18];
    }
}

/// 购车时间
- (void)initWithBuyCarDateView {
    if (!_buyCarDateView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.userPhoneView.bottom;
        _buyCarDateView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_buyCarDateView];
    }
    if (self.taskDetailModel) {
        NSString *str = objectNull(self.taskDetailModel.aakSalesDateValue);
        if (self.taskDetailModel.aakSalesDate == 0) {
            str = @"";
        }
        [_buyCarDateView updateWithViewTitle:@"购车时间" value:str valueHeight:18];
    }
}

/// 行车里程
- (void)initWithMileageView {
    if (!_mileageView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.buyCarDateView.bottom;
        _mileageView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_mileageView];
    }
    if (self.taskDetailModel) {
        NSString *totalMile = [NSString stringWithFormat:@"%.2fkm", self.taskDetailModel.totalMile];
        [_mileageView updateWithViewTitle:@"行车里程" value:totalMile valueHeight:18];
    }
}

/// 维修项目
- (void)initWithRepairItemsView {
    if (!_repairItemsView) {
        CGRect frame = self.serviceTypeView.frame;
        frame.origin.y = self.mileageView.bottom;
        _repairItemsView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
        [self addSubview:_repairItemsView];
    }
    if (self.taskDetailModel) {
        NSMutableArray *addList = [NSMutableArray new];
        if (self.taskDetailModel.maintainItemList && self.taskDetailModel.maintainItemList.count > 0) {
            [addList addObjectsFromArray:self.taskDetailModel.maintainItemList];
        }
        if (self.taskDetailModel.repairItemList && self.taskDetailModel.repairItemList.count > 0) {
            [addList addObjectsFromArray:self.taskDetailModel.repairItemList];
        }
        
        NSMutableString *repairItem = [NSMutableString new];
        if (addList.count > 0) {
            for (RepairItemModel *model in addList) {
                [repairItem appendFormat:@"%@、", model.itemName];
            }
        }
        NSString *repairItemStr = @"";
        if (repairItem.length > 0) {
            repairItemStr = [repairItem substringToIndex:repairItem.length - 1];
        }
        
        [_repairItemsView updateWithViewTitle:@"维修项目" value:repairItemStr valueHeight:18];
    }
}

/// 用户反馈
- (void)initWithFeedbackView {
    if (!_feedbackView) {
        CGRect frame = self.serviceTypeView.frame;
        if (self.taskDetailModel && self.taskDetailModel.detailHeight > 10) {
            frame.size.height = self.taskDetailModel.detailHeight + 10;
        }
        frame.origin.y = self.repairItemsView.bottom;
        _feedbackView = [[WDTaskDetailBasicSingleView alloc] initWithFrame:frame];
//        _feedbackView.backgroundColor = [UIColor orangeColor];
        [self addSubview:_feedbackView];
    }
    if (self.taskDetailModel) {
        [_feedbackView updateWithViewTitle:@"用户反映" value:objectNull(self.taskDetailModel.detail) valueHeight:self.taskDetailModel.detailHeight];
    }
}

- (void)initWitPicImageView {
    if (!_picImageView) {
        CGRect frame = CGRectMake(0, self.feedbackView.bottom + 14, self.width, kWDTaskDetailBasicSingleImageViewHeight);
        _picImageView = [[WDTaskDetailBasicSingleImageView alloc] initWithFrame:frame];
        [self addSubview:_picImageView];
    }
    if (self.taskDetailModel) {
        self.picImageView.top = self.feedbackView.bottom + 14;
        int rows = ceilf(self.taskDetailModel.problemPhotoList.count / 3.0);
        float imgViewHeight = 0;
        if (self.taskDetailModel.problemPhotoList.count > 0) {
            imgViewHeight = rows * 80 + 5 * (rows - 1) - 80;
        }
        self.picImageView.height = kWDTaskDetailBasicSingleImageViewHeight + imgViewHeight;
        [self.picImageView updateWithViewData:self.taskDetailModel];
    }
}


- (void)updateWithViewData:(id)data {
    self.taskDetailModel = data;
    
    // 车牌号码
    [self initWithPlateNumberView];
    
    // 服务类型
    [self initWithServiceTypeView];
    
    // 预约方式
    [self initWithAppointWayView];
    
    // 工单类型
    [self initWithTaskTypeView];
    
    // 当前状态
    [self initWithServFlowView];
    
    // 用户姓名
    [self initWithUserNameView];
    
    // 用户电话
    [self initWithUserPhoneView];
    
    /// 购车时间
    [self initWithBuyCarDateView];

    /// 行车里程
    [self initWithMileageView];

    /// 维修项目
    [self initWithRepairItemsView];

    /// 用户反馈
    [self initWithFeedbackView];
    
    [self initWitPicImageView];
}

@end
